from rest_framework import viewsets, mixins
from rest_framework import permissions
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.authentication import SessionAuthentication
from .serializers import FollowProblemDetailSerializer, FollowProblemSerializer, FavAnswerSerializer, \
    FavAnswerDetailSerializer, AgreeOppositeAnswerSerializer, ProblemToTagSerializer, FollowUserSerializer
from .models import FollowProblem, FavAnswer, AgreeOppositeAnwser, ProblemToTag, FollowUser
from utils.permissions import IsOwnerOrReadOnly
from .filters import ProblemToTagFilter


class FollowProblemViewSet(viewsets.ModelViewSet):
    """
    list:
    获取关注问题的列表
    """
    permission_classes = (IsOwnerOrReadOnly,)
    lookup_field = 'problem_id'  # 这个配置是为了删除关注所做的配置

    def get_queryset(self):
        return FollowProblem.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        instance = serializer.save()
        problem = instance.problem
        problem.follow_num += 1
        problem.save()

    def get_serializer_class(self):
        if self.action == 'list':
            return FollowProblemDetailSerializer
        if self.action == 'create':
            return FollowProblemSerializer
        return FollowProblemDetailSerializer


class FavAnswerViewSet(viewsets.ModelViewSet):
    """
    list:
        获取收藏的答案列表
    retrieve:
        判断某个答案是否收藏
    create:
        添加收藏
    destroy:
        删除收藏
    """
    permission_classes = (permissions.IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    # serializer_class = FavAnswerSerializer
    lookup_field = 'answer_id'  # 这个配置是为了删除以及查询单条记录的id，所做的配置

    def get_queryset(self):
        return FavAnswer.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        instance = serializer.save()
        answer = instance.answer
        answer.fav_count += 1
        answer.save()

    def get_serializer_class(self):
        if self.action == 'list':
            return FavAnswerDetailSerializer
        elif self.action == 'create':
            return FavAnswerSerializer

        return FavAnswerDetailSerializer


class AgreeOppositeAnswerViewSet(mixins.CreateModelMixin, mixins.DestroyModelMixin,
                                 viewsets.GenericViewSet):
    """
    create：
        增加赞同
    destroy:
        删除赞同
    """
    serializer_class = AgreeOppositeAnswerSerializer
    permission_classes = (IsOwnerOrReadOnly,)
    lookup_field = 'answer'

    def get_queryset(self):
        return AgreeOppositeAnwser.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        instance = serializer.save()
        answer = instance.answer
        answer.agree_count += 1
        answer.save()

    def perform_destroy(self, instance):
        answer = instance.answer
        answer.agree_count -= 1
        answer.save()
        instance.delete()


class ProblemToTagViewSet(viewsets.ModelViewSet):
    permission_classes = (IsOwnerOrReadOnly,)
    serializer_class = ProblemToTagSerializer
    filter_backends = (DjangoFilterBackend,)
    filter_class = ProblemToTagFilter

    def get_queryset(self):
        return ProblemToTag.objects.filter(user=self.request.user)


class FollowUserViewSet(viewsets.ModelViewSet):
    serializer_class = FollowUserSerializer
    permission_classes = (IsOwnerOrReadOnly,)
    lookup_field = 'followed_user'
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)

    def get_queryset(self):
        queryset = FollowUser.objects.filter(following_user=self.request.user)
        return queryset
